﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DrugStore.Helper;
using DrugStore.Exceptions;  

namespace DrugStore.Models
{
    #region Model
    public class ProductModel
    {
        public string ProductId { get; set; }
        public string Name { get; set; }
        public string BriefDesc { get; set; }
        public float  CurrentPrice { get; set; }
        public string CategoryName { get; set; }
        public string ImgPath { get; set; }

        public List<Product_Images> imgPaths;

        public ProductModel(Product product) 
        {
            ProductId = product.ProductId.ToString();
            Name = product.ProductName;
            BriefDesc = product.BriefDescription;
            ImgPath = product.ImagePath;
            CategoryName = product.ProductCategory.CategoryName;
            imgPaths = new List<Product_Images>();
            foreach (Product_Images i in product.Product_Images) 
            {
                imgPaths.Add(i);
            }
        }
    }
    #endregion
    #region Services
    public class ProductBussiness 
    {
        DrugStoreEntities entities;
        //Get product by productId        
        public ProductModel GetProductByProductId(string id) 
        {
            entities = new DrugStoreEntities();
            Guid productId = new Guid(id);            

            Product  tempProduct = entities.Products.FirstOrDefault(p => p.ProductId.Equals(productId));            
            if (tempProduct==null) 
            {
                throw new ParameterInvalidException("Null", "Don't exist product");
            }
            ProductModel product = new ProductModel(tempProduct);
            return product;
        }
        //Add new product.
        public void AddProduct() 
        {
        
        }
        //Update product.
        public void UpdateProduct() 
        {
        
        }
        //Delete product.
        public void DeleteProduct() 
        {
        
        }
        //Get all products.
        public PaginateList<Product> GetAllProducts(int pageIndex,int pageSize) 
        {
             entities = new DrugStoreEntities();
             var products = entities.Products.OrderBy(p => p.DateCreate) ;
             PaginateList<Product> productsResult = new PaginateList<Product>(products, pageIndex, pageSize,null,null,null,null);
             return productsResult;
        }
        public IQueryable<Product> GetAllProducts() 
        {
            entities = new DrugStoreEntities();
            return entities.Products.OrderBy(p=>p.DateCreate);
        }
    }
    #endregion

}